﻿//课后作业：
//uniform float 的 uniform作用是》》？？global变量真的不知道什么用
//dot(noiseColor.rgb,float3(0.3,0.59,0.11)) 的 dot是？？点乘，获取2矢量夹角
//(x + 0.5)*0.5 公式作用是？？？ 把-1,-1 变成0,1 ??https://gameinstitute.qq.com/community/detail/103409 (无图无真相）
//#include "UnityPBSLighting.cginc"？？#include "UnityStandardBRDF.cginc"？？
Shader "Unlit/GrassByNoise"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "black" {}
        _Noise("Texture",2D) = "white"{}

        _WaveControl("_WaveControl",vector) = (1,1,1,1)
        _Mask("Mask",2D) = "white"{}
        _Color_01("Color_01",Color) = (1,1,1,1)
        _Color_02("Color_02",Color) = (1,1,1,1)
        _WindControl("_WindControl",vector) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            //#pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float tempCol:TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _Noise;
            float4 _WaveControl;
            uniform sampler2D _Mask; uniform float4 _Mask_ST;
            float4 _Color_01;
            float4 _Color_02;
            float4 _WindControl;
            v2f vert (appdata v)
            {
                v2f o;
                float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
                //_WaveControl.w用来调整地图的大小
                float2 samplePos = worldPos.xz / _WaveControl.w;
                //_WindControl.xyz控制各方向上的速度
                //samplePos += _Time.x * float2(0,_WaveControl.z);//90°朝向自己
                samplePos += _Time.x * _WaveControl.xz;//斜45°角
                fixed waveSample = tex2Dlod(_Noise, float4(samplePos, 0, 0)).r;//斜45°角
                //此公式有点怪。。。。。应该有更好的写法(.w的范围几乎只能在0.2~0.3)
                worldPos.x += sin(waveSample * _WindControl.x) * _WindControl.w * v.uv.y;
                worldPos.z += sin(waveSample * _WindControl.z) * _WindControl.w * v.uv.y;
                o.pos = mul(UNITY_MATRIX_VP, worldPos);
                o.uv = v.uv;
                o.tempCol = waveSample;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float4 _Mask_var = tex2D(_Mask,TRANSFORM_TEX(i.uv, _Mask));
                clip(_Mask_var.r - 0.5);

                float scale = 0.5;
                float3 noiseColor = float3(i.tempCol.x,i.tempCol.x,i.tempCol.x);
                float3 emissive = saturate((pow(lerp(_Color_01.rgb,_Color_02.rgb,_Mask_var.g),2.0)*((dot(noiseColor.rgb,float3(0.3,0.59,0.11))+scale)*scale)));

                fixed4 finalColor = fixed4(emissive,1);
                //fixed4 finalColor = fixed4(frac(i.tempCol.x), 0, 0, 1);
                //fixed4 finalColor = fixed4(lerp(_Color_01.rgb,_Color_02.rgb,_Mask_var.g),1);
                return finalColor;
            }
            ENDCG
        }
    }
}
